package com.opgea.crm.repository;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.opgea.crm.domain.entities.ProjectDetails;
import com.opgea.crm.domain.entities.SetupsValue;
import com.opgea.crm.domain.entities.Task;
import com.opgea.crm.domain.entities.TaskComment;
import com.opgea.crm.domain.entities.security.CrmUser;

@Repository
public class TaskRepoImpl implements TaskRepo {

	@PersistenceContext
	private EntityManager em;

	public Long createTask(Task task) {
		em.persist(task);
		return task.getId();
	}

	
	public Long deleteTask(Long taskid) {
		if(taskid!=null){
		Task entity=em.find(Task.class, taskid);
		//em.remove(entity);
		}
		return taskid;
		}

	
	public List<Task> tasksByProjectId(Long projectid,Long priorityId,Long assigntoId,CrmUser user) {
		
		StringBuffer buffer=new StringBuffer("SELECT entObj FROM Task entObj ");
		String cond=" WHERE ";
		if(projectid!=null)
		{
		 buffer.append(cond);
		 buffer.append(" entObj.projectId.id="+projectid);
		 cond=" AND ";
		}
		if(priorityId!=null)
		{
			buffer.append(cond);
			buffer.append(" entObj.priorityId.id="+priorityId);
			cond=" AND ";
		}
		if(assigntoId!=null)
		{
			buffer.append(cond);
			buffer.append(" entObj.assignTo.id="+assigntoId);
			cond=" AND ";
		}
		if(user!=null)
		{
			buffer.append(cond);
			buffer.append(" entObj.assignTo.id="+user.getEmployee().getId());
		}
		Query query=em.createQuery(buffer.toString(),Task.class);
		List<Task> tasks=query.getResultList();
		return tasks;
	}

	
	public void updateTask(Task task) {
		em.merge(task);

	}
	
	public List<ProjectDetails> allProjectAndMembers(Long projectId)
	{
		
		StringBuffer buffer=new StringBuffer("SELECT entObj FROM ProjectDetails entObj ");
		if(projectId!=null)
		{
			buffer.append(" WHERE entObj.id="+projectId);
		}
		Query query=em.createQuery(buffer.toString(),ProjectDetails.class);
		List<ProjectDetails> list=query.getResultList();
		return list;
	}
	
	public List<SetupsValue> allProjects(){
		
		Query query=em.createQuery("SELECT new com.crm.domain.SetupsValue(entObj.id,entObj.projectName) FROM ProjectDetails entObj");
		List<SetupsValue> list=query.getResultList();
		return list;
	}
	
	public Task findTaskById(Long taskId)
	{
		if(taskId!=null)
			return em.find(Task.class,taskId);
		return null;
	}


	@Override
	public List<Task> tasksListByProjectId(Long projectId) {
		String query = "SELECT t FROM Task t WHERE t.projectId.id = "+projectId;
		List<Task> taskList = em.createQuery(query, Task.class).getResultList();
		return taskList;
	}


	@Override
	public List<TaskComment> findAllCommentsByTaskId(long taskId) {
		String query = "SELECT c FROM TaskComment c where c.task.id =" + taskId; 
		List<TaskComment> issueList =  em.createQuery(query,TaskComment.class).getResultList();
		return issueList;
	}
	
	

}
